package info.uno;

import java.util.ArrayList;

public class Card 
{
	public static String[] cardLabel = {"0,12 B 0 ", "0,12 B 1 ", "0,12 B 2 ", "0,12 B 3 ", "0,12 B 4 ", "0,12 B 5 ", "0,12 B 6 ", "0,12 B 7 ", "0,12 B 8 ", "0,12 B 9 ", "0,12 B Skip ", "0,12 B DrawTwo ", "0,12 B Reverse ",
		"1,8 Y 0 ", "1,8 Y 1 ", "1,8 Y 2 ", "1,8 Y 3 ", "1,8 Y 4 ", "1,8 Y 5 ", "1,8 Y 6 ", "1,8 Y 7 ", "1,8 Y 8 ", "1,8 Y 9 ", "1,8 Y Skip ", "1,8 Y DrawTwo ", "1,8 Y Reverse ",
		"0,3 G 0 ", "0,3 G 1 ", "0,3 G 2 ", "0,3 G 3 ", "0,3 G 4 ", "0,3 G 5 ", "0,3 G 6 ", "0,3 G 7 ", "0,3 G 8 ", "0,3 G 9 ", "0,3 G Skip ", "0,3 G DrawTwo ", "0,3 G Reverse ",
		"0,4 R 0 ", "0,4 R 1 ", "0,4 R 2 ", "0,4 R 3 ", "0,4 R 4 ", "0,4 R 5 ", "0,4 R 6 ", "0,4 R 7 ", "0,4 R 8 ", "0,4 R 9 ", "0,4 R Skip ", "0,4 R DrawTwo ", "0,4 R Reverse ",
		"1,8W0,3I0,4L0,12D", "0,12W1,8I0,3L0,4D0,13 Draw Four "};
	
	public static String[] cardNames = {"b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","bs","bdt","br",
		"y0","y1","y2","y3","y4","y5","y6","y7","y8","y9","ys","ydt","yr",
		"g0","g1","g2","g3","g4","g5","g6","g7","g8","g9","gs","gdt","gr",
		"r0","r1","r2","r3","r4","r5","r6","r7","r8","r9","rs","rdt","rr",
		"w","wdf"};

	public static int B0 = 0;
	public static int B1 = 1;
	public static int B2 = 2;
	public static int B3 = 3;
	public static int B4 = 4;
	public static int B5 = 5;
	public static int B6 = 6;
	public static int B7 = 7;
	public static int B8 = 8;
	public static int B9 = 9;
	public static int BS = 10;
	public static int BDT = 11;
	public static int BR = 12;
	
	public static int Y0 = 13;
	public static int Y1 = 14;
	public static int Y2 = 15;
	public static int Y3 = 16;
	public static int Y4 = 17;
	public static int Y5 = 18;
	public static int Y6 = 19;
	public static int Y7 = 20;
	public static int Y8 = 21;
	public static int Y9 = 22;
	public static int YS = 23;
	public static int YDT = 24;
	public static int YR = 25;
	
	public static int G0 = 26;
	public static int G1 = 27;
	public static int G2 = 28;
	public static int G3 = 29;
	public static int G4 = 30;
	public static int G5 = 31;
	public static int G6 = 32;
	public static int G7 = 33;
	public static int G8 = 34;
	public static int G9 = 35;
	public static int GS = 36;
	public static int GDT = 37;
	public static int GR = 38;
	
	public static int R0 = 39;
	public static int R1 = 40;
	public static int R2 = 41;
	public static int R3 = 42;
	public static int R4 = 43;
	public static int R5 = 44;
	public static int R6 = 45;
	public static int R7 = 46;
	public static int R8 = 47;
	public static int R9 = 48;
	public static int RS = 49;
	public static int RDT = 50;
	public static int RR = 51;
	
	public static int W = 52;
	public static int WDF = 53;
	
	private int cardType;
	
	public Card(int type)
	{
		this.cardType = type;
	}
	
	public String getLabel()
	{
		return this.cardLabel[this.cardType];
	}
	
	public static boolean isValidCard(Card c)
	{
		if(c != null)
		{
			System.out.println("Not null");
			
			for(int i = 0; i < cardNames.length; i++)
			{
				if(c.getCardName().equals(cardNames[i]))
				{
					return true;
				}
			}
			
		}
		System.out.println("null");
		return false;
	}
	
	public int getType()
	{
		return this.cardType;
	}
	
	public String getCardName()
	{
		return this.cardNames[this.cardType];
	}
	
	public static Card stringToCard(String s)
	{
		for(int i = 0; i < cardNames.length; i++)
		{
			if(s.equals(cardNames[i]))
			{
				return new Card(i);
			}
		}
		return new Card(-1);
	}
	
	public static Card findCardTypeInArrayList(ArrayList<Card> c, String type)
	{
		int i = 0;
		
		for(Card cc : c)
		{
			if(cc.getCardName().equals(type))
			{
				System.out.println("found");
				return c.get(i);
			}
			
			System.out.println("i " + i);
			
			i++;
		}
		return null;
	}
	
	public static int stringToCardNumber(String s)
	{
		for(int i = 0; i < cardNames.length; i++)
		{
			if(s.equals(cardNames[i]))
			{
				return i;
			}
		}
		
		return -1;
	}
	
	public String getColor()
	{
		if(cardType >= 0 && cardType <= 12)
		{
			return "B";
		}
		else if(cardType >= 13 && cardType <= 25)
		{
			return "Y";
		}
		else if(cardType >= 26 && cardType <= 38)
		{
			return "G";
		}
		else if(cardType >= 39 && cardType <= 51)
		{
			return "R";
		}
		else
		{
			return "W";
		}
	}
	
	public static boolean compareColor(Card c, Card c2)
	{
		if(c.getColor() == "W" || c2.getColor() == "W")
		{
			return true;
		}
		else
		{
			return c.getColor().equals(c2.getColor());
		}
	}
	
	public void setType(int type)
	{
		this.cardType = type;
	}
	
	public static boolean compareNumber(Card c, Card c2)
	{
		
		int ctempType = 0;
		int tempType = 0;
		
		if(c.getColor().equals("B"))
		{
			tempType = c.cardType;
		}
		else if(c.getColor().equals("Y"))
		{
			tempType = c.cardType - 13;
		}
		else if(c.getColor().equals("G"))
		{
			tempType = c.cardType - 26;
		}
		else if(c.getColor().equals("R"))
		{
			tempType = c.cardType - 39;
		}
		else
		{
			return true;
		}
		
		if(c2.getColor().equals("B"))
		{
			ctempType = c2.getType();
		}
		else if(c2.getColor().equals("Y"))
		{
			ctempType = c2.getType() - 13;
		}
		else if(c2.getColor().equals("G"))
		{
			ctempType = c2.getType() - 26;
		}
		else if(c2.getColor().equals("R"))
		{
			ctempType = c2.getType() - 39;
		}
		else
		{
			return true;
		}
		
		return tempType == ctempType;
	}
	
	public static boolean compareCards(Card c, Card c2)
	{
		if(!(c == null) && !(c2 == null))
		{
			return compareColor(c,c2) || compareNumber(c,c2);
		}
		else
		{
			return false;
		}
	}
	
	public static ArrayList<Card> sort(ArrayList<Card> c)
	{
		for (int i = 1; i < c.size(); i++)
	      {
	         int next = c.get(i).getType();
	         
	         int j = i;
	         
	         while (j > 0 && c.get(j - 1).getType() > next)
	         {
	            c.get(j).setType(c.get(j-1).getType());
	            j--;
	         }
	         
	         c.get(j).setType(next);
	      }

		return c;
	}
}

